using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Map2.Simul;

namespace Map2
{
    public partial class Form1 : Form
    {
        private Map map;
        private Simulation simul;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            map = new Map(new Size((int)mapWidth.Value, (int)mapHeight.Value));
            map.setPanel(mapPanel1);
            resizeForm();
            trackBar1.Value = 10;
            setDrawProperties();
        }

        private void mapWidth_ValueChanged(object sender, EventArgs e)
        {
            map.setSize(new Size((int)mapWidth.Value, map.getSize().Height));
        }

        private void mapHeight_ValueChanged(object sender, EventArgs e)
        {
            map.setSize(new Size(map.getSize().Width, (int)mapHeight.Value));
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            resizeForm();
        }

        private void resizeForm()
        {
            containerMap.Location = new Point(50, 50);
            containerMap.Width = this.Width - 200;
            containerMap.Height = this.Height - 150;
            panel1.Location = new Point(this.Width - 150, 50);
            setDrawProperties();
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "Open";
            open.Filter = "Map Files (*.Xml)|*.Xml";

            if (open.ShowDialog() == DialogResult.OK)
                map.loadMap(open.FileName);
            mapWidth.Value = new decimal(map.getSize().Width);
            mapHeight.Value = new decimal(map.getSize().Height);
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            setDrawProperties();
            label3.Text="Zoom :"+trackBar1.Value+"/10";
        }

        private void containerMap_Scroll(object sender, ScrollEventArgs e)
        {
            setDrawProperties();
        }

        private void setDrawProperties()
        {
            mapPanel1.setZoom((double)trackBar1.Value / 10);
            mapPanel1.setDrawProperties(containerMap.Size, new Point(-containerMap.AutoScrollPosition.X, -containerMap.AutoScrollPosition.Y));
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save As ...";
            save.Filter = "Map Files (*.Xml)|*.Xml";

            if (save.ShowDialog() == DialogResult.OK)
                map.saveMap(save.FileName);
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            map = new Map(new Size((int)mapWidth.Value, (int)mapHeight.Value));
            map.setPanel(mapPanel1);
            resizeForm();
            trackBar1.Value = 10;
            setDrawProperties();
        }

        private void testN1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            simul = new Simulation(map);
            simul.start();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(simul!=null)
                simul.stop();
        }

    }
}